VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "CContractDTLCol"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"No"
Attribute VB_Ext_KEY = "Collection" ,"CContractDetail"
Attribute VB_Ext_KEY = "Member0" ,"CContractDetail"
Option Explicit

'local variable to hold collection
Private mCol As Collection

Private msErrorSource As String
Public Function Add(mMoney As Double, mPrice As Double, mQuantity As Double, mProcessNO As String, mPackageID As Long, mSpecID As Long, mProductID As Long, mOrderNum As Integer, mContractID As Long, mUnitID As Long, Optional sKey As String) As CContractDetail
    'create a new object
    Dim objNewMember As CContractDetail
    Set objNewMember = New CContractDetail


    'set the properties passed into the method
    objNewMember.mMoney = mMoney
    objNewMember.mPrice = mPrice
    objNewMember.mQuantity = mQuantity
    objNewMember.mProcessNO = mProcessNO
    objNewMember.mPackageID = mPackageID
    
    If UI.ZDL(gnEnum_ZDL_PrdSpec).Nodes.Exists(mSpecID) Then
        objNewMember.mSpecID = mSpecID
    Else
        objNewMember.mSpecID = gnNULLID
    End If
    
    If UI.ZDL(gnEnum_ZDL_Product).Nodes.Exists(mProductID) Then
        objNewMember.mProductID = mProductID
    Else
        objNewMember.mProductID = gnNULLID
    End If
    
    objNewMember.mOrderNum = mOrderNum
    objNewMember.mContractID = mContractID
    objNewMember.mUnitID = mUnitID
 
    If Len(sKey) = 0 Then
        mCol.Add objNewMember
    Else
        mCol.Add objNewMember, sKey
    End If


    'return the object created
    Set Add = objNewMember
    Set objNewMember = Nothing


End Function

Public Property Get Item(vntIndexKey As Variant) As CContractDetail
Attribute Item.VB_UserMemId = 0
    'used when referencing an element in the collection
    'vntIndexKey contains either the Index or Key to the collection,
    'this is why it is declared as a Variant
    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)
  Set Item = mCol(vntIndexKey)
End Property



Public Property Get Count() As Long
    'used when retrieving the number of elements in the
    'collection. Syntax: Debug.Print x.Count
    Count = mCol.Count
End Property


Public Sub Remove(vntIndexKey As Variant)
    'used when removing an element from the collection
    'vntIndexKey contains either the Index or Key, which is why
    'it is declared as a Variant
    'Syntax: x.Remove(xyz)


    mCol.Remove vntIndexKey
End Sub


Public Property Get NewEnum() As IUnknown
Attribute NewEnum.VB_UserMemId = -4
Attribute NewEnum.VB_MemberFlags = "40"
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property


Private Sub Class_Initialize()
    'creates the collection when this class is created
    Set mCol = New Collection
End Sub


Private Sub Class_Terminate()
    'destroys collection when this class is terminated
    Set mCol = Nothing
End Sub

Public Function Load(ByVal ContractID As Long) As Long
Dim SQL As String
Dim Rst As ADODB.Recordset
Dim lrtn As Long
    
    msErrorSource = sprintf("CContractDTLCol.Load(),ContractID=%d", ContractID)

    SQL = sprintf("Select * From D_ContractDetail Where ContractID =%d Order by OrderNum", ContractID)
    Set Rst = UI.Svr.cntMain.Execute(SQL, , adCmdText)
    Set mCol = New Collection
    
    Do While Not Rst.EOF
        With Rst
            Call Add(.Fields("Money").Value, .Fields("Price").Value, .Fields("Quantity").Value, CStr(.Fields("ProcessNO").Value), _
                 .Fields("PackageID").Value, .Fields("SpecID").Value, .Fields("ProductID").Value, .Fields("OrderNum").Value, _
                 .Fields("ContractID").Value, .Fields("UnitID").Value)
        End With
        Rst.MoveNext
    Loop
    
    
    lrtn = gnSUCCESS
ExitEntry:
    Load = lrtn
    Exit Function
ErrorAttach:
    lrtn = Err.Number
    UI.Svr.Err.Push lrtn, msErrorSource, Err.Description
    Resume ExitEntry
End Function

Public Function Save(ByVal ContractID As Long) As Long
On Error GoTo ErrorAttach
Dim CmdContractDetail As ADODB.Command
Dim SQL As String
Dim tmpDetailItem As CContractDetail
Dim RecordsAffected As Integer
Dim lrtn As Long
    
    msErrorSource = sprintf("CContractDTLCol.Save()")
    Set CmdContractDetail = New ADODB.Command
    CmdContractDetail.CommandType = adCmdText
    Set CmdContractDetail.ActiveConnection = UI.Svr.cntMain
    
    UI.Svr.cntMain.Execute "Delete From D_ContractDetail where ContractID=" & ContractID, RecordsAffected
    For Each tmpDetailItem In mCol
        If (UI.ZDL(gnEnum_ZDL_Product).Nodes.Exists(tmpDetailItem.mProductID)) Then
            If (UI.ZDL(gnEnum_ZDL_PrdSpec).Nodes.Exists(tmpDetailItem.mSpecID)) Then
            Else
                tmpDetailItem.mSpecID = gnNULLID
            End If
            
            With tmpDetailItem
                    SQL = sprintf("Insert Into D_ContractDetail (ContractID,OrderNum,ProductID,SpecID,PackageID,ProcessNo,UnitID,Quantity,Price,Money) Values(%d,%d,%d,%d,%d,'%s',%d,%d,%d,%d)", _
                            .mContractID, .mOrderNum, .mProductID, .mSpecID, .mPackageID, .mProcessNO, .mUnitID, .mQuantity, .mPrice, .mMoney)
            End With
            Debug.Print SQL
            CmdContractDetail.CommandText = SQL
            CmdContractDetail.Execute
        End If
    Next
    lrtn = gnSUCCESS
ExitEntry:
    Save = lrtn
    Exit Function
ErrorAttach:
    lrtn = Err.Number
    UI.Svr.Err.Push lrtn, msErrorSource, Err.Description
    Resume ExitEntry
End Function
